#include "preset_control/preset_control.hpp"

#ifdef DBG_TAG
#undef DBG_TAG
#endif
#ifdef DBG_LVL
#undef DBG_LVL
#endif

#define DBG_TAG "preset_ctl"
#define DBG_LVL DBG_LOG

extern "C"
{
#include <rtdbg.h>
}
/******************************************************************************
 *
 * @brief:    数据储存预初始化
 *
 ******************************************************************************/
rt_thread_t record_tid = NULL;

void global_data_factory_thread_entry(void * pra)
{
    global_data->DATA_SAVE_factory_init();
    rt_hw_cpu_reset();
}
int global_data_device_factory_init()
{
    rt_thread_t tid;
    LOG_W("stop record_tid to erase");

    if(record_tid)
        rt_thread_delete(record_tid);

    tid = rt_thread_create("gl_fac", global_data_factory_thread_entry, RT_NULL, 4096, 9, 200);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("gl_fac thread not start!");
    return 0;
}
MSH_CMD_EXPORT(global_data_device_factory_init, global_data_factory_init);


static void global_data_device_registe_value()
{
    global_data->DATA_SAVE_short_cycle_registe_data("IN_V", data_save_type_float_t);
}

data_save_status global_data_device_init_interface()
{
    global_data_device_registe_value();
//    global_data.DATA_SAVE_open_debug();
    global_data->DATA_SAVE_init();
    return global_data->DATA_SAVE_read_init_status();
}

/******************************************************************************
 *
 * @brief:    超时波初始化
 *
 ******************************************************************************/
/* 接收数据回调函数 */
rt_err_t ultra_sonic_uart_input_callback(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */

    ultra_sonic_uart->CHIP_UART_rx_default_callback();
    return RT_EOK;
}

void ultra_sonic_uart_rx_thread_entry(void *p)
{
    ultra_sonic_uart->CHIP_UART_rx_default_thread_handler();
}

chip_uart_status ultra_sonic_uart_init()
{
    rt_thread_t tid;
    chip_uart_status flag;
    flag = ultra_sonic_uart->CHIP_UART_read_init_status();
    if(flag != chip_uart_status_ok)
        return chip_uart_status_fail;

    flag = ultra_sonic_uart->CHIP_UART_init_rx_interrupt(ultra_sonic_uart_input_callback);
    if(flag != chip_uart_status_ok)
        return chip_uart_status_fail;

    tid = rt_thread_create("sonic_rx", ultra_sonic_uart_rx_thread_entry, RT_NULL, 2048, 10, 10);
    if (tid != RT_NULL) rt_thread_startup(tid);
    else return chip_uart_status_fail;

}




